package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.ExerciseReport;
import com.jrtc.base.entity.vo.ExerciseReportVo;
import com.jrtc.base.entity.vo.InsertReportVo;
import com.jrtc.service.ExerciseReportService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.ValidationEvent;
import java.util.Map;

/**
 *  运动记录报告表(ExerciseReport)表控制层
 *
 * @author makejava
 * @since 2022-09-07 15:54:10
 */
@RestController
@RequestMapping("exerciseReport")
public class ExerciseReportController {
    /**
     * 服务对象
     */
    @Resource
    private ExerciseReportService exerciseReportService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ExerciseReportVo selectOne(Integer id) {
        return this.exerciseReportService.queryById(id);
    }
    
    @RequestMapping("getExerciseReportList")
    public ResultDTO getExerciseReportList(@RequestBody ExerciseReport exerciseReport)
    {
        IPage<ExerciseReport> ipage = exerciseReportService.queryExerciseReportList(exerciseReport);
        return ResultDTOBuilder.success(ipage);
    }

    /**
     * 添加报告
     * */
    @RequestMapping("insertExerciseReport")
    public ResultDTO insertExerciseReport(@RequestBody ExerciseReportVo exerciseReport) {
        InsertReportVo insert;
        try {
            insert = exerciseReportService.insert(exerciseReport);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(insert);
    }

    @RequestMapping("getExerciseReportById")
    public ResultDTO getExerciseReportById(@RequestBody Map<String, String> map)
    {
        String exerciseReportId = map.get("exerciseReportId");
        ExerciseReportVo exerciseReport = exerciseReportService.queryById(Integer.parseInt(exerciseReportId));
        if (Strings.isNullOrEmpty(exerciseReportId) || exerciseReport==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(exerciseReport);
    }

    @RequestMapping("updateExerciseReport")
    public ResultDTO updateExerciseReport(@RequestBody ExerciseReport exerciseReport)
    {
        if (exerciseReport==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (exerciseReportService.update(exerciseReport)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteExerciseReport")
    public ResultDTO deleteExerciseReport(@RequestBody Map<String,String> map)
    {
        String exerciseReportId = map.get("exerciseReportId");
        if (!Strings.isNullOrEmpty(exerciseReportId))
        {
            if (exerciseReportService.deleteById(Integer.parseInt(exerciseReportId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }

    /**
     * 运动记录图表
     * */
    @PostMapping("/report")
    public ResultDTO report(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        ExerciseReportVo exerciseReportVo = exerciseReportService.report(id);
        return ResultDTOBuilder.success(exerciseReportVo);
    }
}